//
//  QTNewFuture.m
//  QTLifeCircle
//
//  Created by 王琼 on 17/1/19.
//  Copyright © 2017年 徐腾. All rights reserved.
// guide_2

#import "QTNewFuture.h"
#import <Masonry.h>

//代理
@interface QTNewFuture ()<UIScrollViewDelegate>

//图片轮播器作为属性
@property(nonatomic, weak) UIScrollView * picScroll;

@property(nonatomic,weak)UIPageControl* pageCtrl;

@end

@implementation QTNewFuture

-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        [self setupUI];
    }
    return self;
}



//重写images setter方法
-(void)setImags:(NSArray *)images
{
    images = images;
    CGRect rect = self.picScroll.bounds;
    
    CGFloat width = rect.size.width;
    
    for(int i = 0;i< images.count;i++)
    {
        UIImageView* iv =[[UIImageView alloc]init];
        
        //> 设置frame
        iv.frame = CGRectOffset(rect, i * width , 0);
        
        //> 设置图片
        iv.image = images[i];
        
        //> 添加到scrollView
        [self.picScroll addSubview:iv];
    }
    
    //设置scrollView的宽高
    self.picScroll.contentSize = CGSizeMake((images.count+1)* width,0);
    
    
    //设置分页指示器的页数
    self.pageCtrl.numberOfPages = images.count;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int page =(scrollView.contentOffset.x)/(scrollView.bounds.size.width)+0.5;
    self.pageCtrl.currentPage = page;
    
    //如果是最后一页并且翻出主页面一半，我们就隐藏分页指示器
    if(page == self.pageCtrl.numberOfPages){
        
        self.pageCtrl.hidden = YES;//设置是否隐藏，YES为隐藏，NO为不隐藏
        
    }else{
        
        self.pageCtrl.hidden = NO;
    }
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    int page =(scrollView.contentOffset.x)/(scrollView.bounds.size.width)+0.5;
    self.pageCtrl.currentPage = page;
    
    //如果是最后一页并且翻出主页面一半，我们就隐藏分页指示器
    if(page == self.pageCtrl.numberOfPages){
        [[UIApplication sharedApplication]setStatusBarHidden:NO];
        [self removeFromSuperview];
    }
}

#pragma mark - 初始化控件方法

-(void)setupUI{
    
    //----创建UIScrollView
    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:self.bounds];
    
    //    sv.backgroundColor = [UIColor blueColor];
    
    //添加到父容器
    [self addSubview:sv];
    
    
    //> 禁用弹簧效果
    sv.bounces = NO;
    
    //> 禁用水平滚动条 （如果不禁用，即使不显示它也存在）
    sv.showsHorizontalScrollIndicator = NO;
    //> 禁用垂直滚动条
    sv.showsVerticalScrollIndicator = NO;
    
    //> 开启分页效果
    sv.pagingEnabled = YES;
    
    //> 设置当前视图成为scrollView的代理
    sv.delegate = self;
    
    
    //记录变量
    self.picScroll = sv;
    
    
    
    //----创建UIPageControl
    
    UIPageControl *pgCtrl = [[UIPageControl alloc] init];
    
    //>设置属性（总页数）
    //    pgCtrl.numberOfPages = 7;//不能写死，应该根据图片数量来
    //设置当前所在页
    pgCtrl.currentPage = 0;
    
    //    //设置默认颜色
    //    pgCtrl.pageIndicatorTintColor = [UIColor blueColor];
    //    //设置当前页颜色
    //    pgCtrl.currentPageIndicatorTintColor = [UIColor orangeColor];
    
    //> 添加到新特性界面
    [self addSubview:pgCtrl];
    
    //> 禁用用户交互
    pgCtrl.userInteractionEnabled = NO;
    
    //> 设置约束（UIPageControl可以根据它自身的内容算出宽度和高度，我们只要给位置）
    [pgCtrl mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(-80);
    }];
    
    
    //属性记录pageControl
    self.pageCtrl = pgCtrl;
    
}

@end



/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */


